package com.magicodex.bookkeeping.biz.setting.event;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Toast;

import com.magicodex.bookkeeping.R;
import com.magicodex.bookkeeping.biz.accounting.constant.AccountingConstants;
import com.magicodex.bookkeeping.biz.accounting.dto.AccountingDTO;
import com.magicodex.bookkeeping.biz.accounting.util.AccountingCsvUtil;
import com.magicodex.bookkeeping.biz.accounting.util.AccountingDataUtil;
import com.magicodex.bookkeeping.biz.accounting.util.AccountingJsonUtil;
import com.magicodex.bookkeeping.biz.common.util.CheckUtil;
import com.magicodex.bookkeeping.biz.common.util.FileUtil;
import com.magicodex.bookkeeping.biz.common.util.StringUtil;

import java.util.List;

/**
 * @author mh.z
 */
public class ImportButtonClickListener implements View.OnClickListener {
    private Activity activity;

    public ImportButtonClickListener(Activity activity) {
        this.activity = activity;
    }

    @Override
    public void onClick(View view) {
        ClipboardManager clipboardManager = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = clipboardManager.getPrimaryClip();

        // 如果剪贴板中无数据，则提示请复制要导入的数据
        if (clipData.getItemCount() <= 0) {
            Toast toast = Toast.makeText(activity, R.string.msg_prompt_copy_import_data, Toast.LENGTH_SHORT);
            toast.show();
            return;
        }

        List<AccountingDTO> recordList = null;
        String dialogContent = null;
        boolean checkFailed = false;
        // 获取剪贴板中数据
        ClipData.Item clipDataItem = clipData.getItemAt(0);
        String clipContent = StringUtil.toString(clipDataItem.getText());

        try {
            // 解析字符串
            recordList = AccountingCsvUtil.parseAccountingDTOs(clipContent);
            // 生成对话框内容
            dialogContent = AccountingCsvUtil.stringifyAccountingDTOs(recordList);
            // 对记录进行排序
            AccountingDataUtil.sortAccountingRecords(recordList);
        } catch (Exception e) {
            checkFailed = true;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);

        if (checkFailed) {
            builder.setTitle(R.string.dialog_title_cannot_import);
            builder.setMessage(clipContent);

            builder.setNegativeButton(R.string.button_cancel, createNegativeButtonClickListener());
        } else {
            builder.setTitle(R.string.dialog_title_import_data);
            builder.setMessage(dialogContent);

            builder.setPositiveButton(R.string.button_import, createPositiveButtonClickListener(recordList));
            builder.setNegativeButton(R.string.button_cancel, createNegativeButtonClickListener());
        }

        AlertDialog dialog = builder.create();
        // 显示对话框
        dialog.show();
    }

    /**
     * 点击确定按钮的事件监听器
     *
     * @return
     */
    protected DialogInterface.OnClickListener createPositiveButtonClickListener(List<AccountingDTO> records) {
        CheckUtil.notNull(records, "records null");

        return (dialog, which) -> {
            String json = AccountingJsonUtil.stringifyAccountingDTOs(records);
            FileUtil.writeFile(activity, AccountingConstants.DATA_PATH, json);

            // 提示导入成功
            Toast toast = Toast.makeText(activity, R.string.msg_import_success, Toast.LENGTH_SHORT);
            toast.show();
        };
    }

    /**
     * 点击取消按钮的事件监听器
     *
     * @return
     */
    protected DialogInterface.OnClickListener createNegativeButtonClickListener() {
        return (dialog, which) -> {
            //
        };
    }

}
